package com.bw.a1908.ui.main;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.LogUtils;
import com.bw.a1908.R;
import com.bw.a1908.core.mvp.BaseFragment;
import com.bw.a1908.core.mvp.IPresenter;
import com.bw.a1908.ui.video.CardVideoFragment;
import com.bw.a1908.ui.video.VideoFullScreenFragment;
import com.bw.a1908.ui.video.VideoItemFragment;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.appbar.AppBarLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * TabLayout + ViewPager + Fragment (列表)
 */
public class MainFragment extends BaseFragment<IPresenter> {

    private SlidingTabLayout mTab;
    private CoordinatorLayout coordinatorLayout;
    private ViewPager mPager;
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private AppBarLayout appBarLayout;
    private ImageView bannerView;

    @Override
    public int bandLayout() {
        return R.layout.fragment_main;
    }

    @Override
    public void initView() {
        mTab = findViewById(R.id.fragment_main_tab);
        mPager = findViewById(R.id.fragment_main_pager);
        appBarLayout = findViewById(R.id.fragment_app_bar_layout);
        coordinatorLayout = findViewById(R.id.fragment_main_layout);
        bannerView = findViewById(R.id.fragment_banner);
    }

    @Override
    public void initData() {
        fragments.add(new CardVideoFragment());
        fragments.add(new VideoItemFragment());
        fragments.add(new VideoFullScreenFragment());
        mPager.setAdapter(new FragmentVideoAdapter(getChildFragmentManager()));
        mTab.setViewPager(mPager, new String[]{"关注", "推荐", "精选"});


        mTab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 2) {
                    scrollToTop();
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    scrollToTop();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void scrollToTop() {
        //拿到 appbar 的 behavior,让 appbar 滚动
        ViewGroup.LayoutParams layoutParams = appBarLayout.getLayoutParams();
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) layoutParams).getBehavior();
        if (behavior instanceof AppBarLayout.Behavior) {
            AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
            //拿到下方tabs的y坐标，即为我要的偏移量
            float y = mTab.getY();
            //注意传递负值
            appBarLayoutBehavior.setTopAndBottomOffset((int) -y);
        }
    }

    class FragmentVideoAdapter extends FragmentStatePagerAdapter {

        public FragmentVideoAdapter(@NonNull FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
